package com.mh.www.mh_frame.app.application;

import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;

import com.mh.www.mh_frame.model.db.manager.dbmanager.AbstractDatabaseManager;
import com.mh.www.mh_frame.util.OkHttpUtil;
import com.mh.www.mh_frame.util.okhttp.cache.SetCookieCache;
import com.mh.www.mh_frame.util.okhttp.https.HttpsUtils;
import com.mh.www.mh_frame.util.okhttp.log.LoggerInterceptor;
import com.mh.www.mh_frame.util.okhttp.persisstent.ClearableCookieJar;
import com.mh.www.mh_frame.util.okhttp.persisstent.PersistentCookieJar;
import com.mh.www.mh_frame.util.okhttp.persistence.SharedPrefsCookiePersistor;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;

import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import okhttp3.OkHttpClient;

/**
 * Created by think on 2018/1/3.
 */

public class MyApplication extends Application {
    private static Context applicationContext;

    FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
            .tag("MH").build();


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        applicationContext = getApplicationContext();
        AbstractDatabaseManager.initOpenHelper(getApplicationContext());//初始化数据库
        ClearableCookieJar cookieJar1 = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(this.getApplicationContext()));
//            Fresco.initialize(getApplicationContext());
        HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);
//            CookieJarImpl cookieJar1 = new CookieJarImpl(new MemoryCookieStore());
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                .addInterceptor(new LoggerInterceptor("TAG"))
                .cookieJar(cookieJar1)
                .hostnameVerifier(new HostnameVerifier()
                {
                    @Override
                    public boolean verify(String hostname, SSLSession session)
                    {
                        return true;
                    }
                })
                .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
                .build();
        OkHttpUtil.initClient(okHttpClient);

        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy) {
            @Override public boolean isLoggable(int priority, String tag) {
                return true;
//                return BuildConfig.DEBUG; //上线后
            }
        });
    }


   public static Context getAppContext(){
        return applicationContext;
   }


}
